package com.py.korona.ui.activities.plan;

import android.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.py.korona.R;
import com.py.korona.adapters.CustomListAdapterMapping;
import com.py.korona.adapters.CustomListAdapterWrapper;
import com.py.korona.db.UriBuilder;
import com.py.korona.db.model.GeoplanEntity;

public class PlanListFragment extends Fragment {
    private static final int ACTIVITY_CREATE = 2;
    private static final int ACTIVITY_EDIT   = 1;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        /*
         * 
         */
        CustomListAdapterWrapper planListAdapterWrapper_ = new CustomListAdapterWrapper();
        planListAdapterWrapper_.setMapping(new CustomListAdapterMapping() {
            @Override
            public String[] bindFromColumns() {
                return new String[] { GeoplanEntity.FieldNameText, GeoplanEntity.FieldNameImageData };
            }

            @Override
            public int bindToLayout() {
                return R.layout.cell_plan_list;
            }

            @Override
            public int[] bindToLayoutViews() {
                return new int[] { R.id.ListCellPlanEditName, R.id.ListCellPlanImage };
            }

            @Override
            public String[] contentProperties() {
                return new String[] { GeoplanEntity.TableFieldAliasID(), GeoplanEntity.TableFieldAliasText(), GeoplanEntity.TableFieldAliasImageData() };
            }

            @Override
            public Uri contentURI() {
                return UriBuilder.objectListUri(GeoplanEntity.uriAuthority(), GeoplanEntity.uriBasePath());
            }

            @Override
            public ViewBinder binder() {
                // return new RoomListBinder(imageCache);
                return null;
            }

            @Override
            public String filter() {
                return null;
            }

            @Override
            public String[] filterArgs() {
                return null;
            }
        });
        planListAdapterWrapper_.setContext(getActivity());
        getLoaderManager().initLoader(0, null, planListAdapterWrapper_);
        /*
         * 
         */
        OnItemClickListener onItemClickListener = new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Cursor cursor = (Cursor) adapterView.getItemAtPosition(position);
                if (cursor.moveToFirst()) {
                    onItemOpenClick(id);
                }
            }
        };
        /*
         * 
         */
        ListView listView_ = (ListView) getActivity().findViewById(R.id.plan_listview1);
        listView_.setAdapter(planListAdapterWrapper_.adapter());
        listView_.setOnItemClickListener(onItemClickListener);
        listView_.setScrollingCacheEnabled(false);
        this.registerForContextMenu(listView_);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.activity_plan_list, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_plan_list_container, null);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        boolean result;
        switch (item.getItemId()) {
            case R.id.PlanAddMenuItem: {
                onItemAddClick();
                result = true;
                break;
            }
            default: {
                result = super.onOptionsItemSelected(item);
                break;
            }
        }
        return result;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    private void onItemAddClick() {
        Intent intent = new Intent();
        intent.setClass(getActivity(), PlanDetailActivity.class);
        intent.putExtra("readonly", false);
        startActivityForResult(intent, ACTIVITY_CREATE);
    }

    private void onItemOpenClick(long id) {
        // ContentValues cv = new ContentValues();
        // cv.put(GeoplanEntity.FieldNameID, id);
        // Uri uri = UriBuilder.objectFilterUri(GeoplanEntity.uriAuthority(),
        // GeoplanEntity.uriBasePath(), cv);
        // String contentType =
        // UriBuilder.contentType(GeoplanEntity.uriBasePath());
        // Intent intent = new Intent(getActivity(), PlanDetailActivity.class);
        // intent.putExtra("readonly", true);
        // intent.putExtra(contentType, uri);
        // startActivityForResult(intent, ACTIVITY_EDIT);
    }
}
